package com.my.jwt.shiro.test1;

import com.louislivi.fastdep.shirojwt.jwt.JwtUtil;

import com.my.cache.bean.CacheBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author timo 2021/3/19
 */
@RestController
@Slf4j
public class TestController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    @Qualifier("redisCacheBean")
    protected CacheBean cacheBean;
    /**
     * 当前为示例所以直接返回了token，真实环境为校验登录信息后再返回token即可
     * @author : louislivi
     */
    @GetMapping("front/login")
    public String login() {
        // ...校验登录信息是否正确
        // 传入用户唯一标示
        String sign = jwtUtil.sign("1");
        cacheBean.put("token",sign,60);
        return sign;
    }

    /**
     * 当前为示例所以权限写的是用户ID 真实环境替换为权限key
     * @author : louislivi
     */
    @GetMapping("admin")
    @RequiresPermissions("1")
    public String jwt() {
        Object token = cacheBean.get("token");
        log.info(token+"=================token");
        return "ok!";
    }
}
